function $$(id)
{
    return document.getElementById(id);
}

function show(items, index)
{
    var pswpElement = $$("pswp");
    var options = {index: index, shareEl: false};
    var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
    gallery.init();
}

function HTMLEncode(html)
{
    var div = document.createElement("div");
    if(div.textContent != null){
        div.textContent = html;
    }
    else{
        div.innerText = html;
    }
    return div.innerHTML;
}

function doCode(html)
{
    var reg = /<code>([\s\S]*?)<\/code>/gi;
    while(res = reg.exec(html)){
        html = html.replace(res[0], '<pre class="layui-code" lay-title="代码" lay-height="200" lay-skin="" lay-encode="">' + HTMLEncode(res[1]) + '</pre>');
    }
    return html;
}

function sendGetRequest(url, successCallback, failCallback)
{
    function success(response)
    {
        if(response.ok == true && response.status == 200){
            response.json().then(function(json){
                successCallback(json);
            });
        }
    }

    function fail(response)
    {
        if(typeof(failCallback) == "function"){
            failCallback(response);
        }
        else{
            console.log(response);
        }
    }

    fetch(url).then(success).catch(fail);
}

function getCookie(name)
{
    var result = {};
    var list = document.cookie.split("; ");
    for(var i in list){
        var item = list[i];
        var kv = item.split("=");
        result[kv[0]] = kv[1];
    }
    if(name == ""){
        return result;
    }
    return result[name];
}

function isLogin(isLoginUrl, loginUrl, span)
{
    function success(json)
    {
        if(json.code > 0){
            setTimeout(show, span);
        }
        else{
            setTimeout(check, span);
        }
    }

    function check()
    {
        var token = getCookie("token");
        if(!token){
            setTimeout(show, span);
        }
        else{
            sendGetRequest(isLoginUrl + "?device=html&token=" + token, success, function(){});
        }
    }

    function show()
    {
        layer.open({
            icon: 2,
            closeBtn: 0,
            content: '请登录后再操作',
            yes: function(){
                window.location.href = loginUrl;
            }
        });
    }

    check();
}

function httpBuildQuery(data)
{
    var list = [];
    for(var key in data){
        var value = data[key];
        list.push(key + "=" + value);
    }
    return list.join("&");
}

function list(y, boxId, pageSizeBoxId, count, limit, makeUrlCallback, code)
{
    function success(json)
    {
        if(!json.data.list.length){
            return false;
        }
        laytpl(code).render(json.data, function(html){
            $("#" + boxId).html(html);
        });
    }

    var $ = y.jquery;
    var laypage = y.laypage;
    var laytpl = y.laytpl;

    laypage.render({
        elem: pageSizeBoxId,
        count: count,
        curr: 1,
        limit: limit,
        theme: '#1E9FFF',
        layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
        jump: function(obj, first){
            var url = makeUrlCallback(obj.curr, obj.limit);
            sendGetRequest(url, success);
        }
    });
}

function getOrderListCode()
{
    var code = `
    {{#  layui.each(d.list, function(index, item){ }}
    <tr>
        <td>{{ item.id }}</td>
        <td>{{ item.product.dry_name }}</td>
        <td><img src="{{ d.oss_domain }}/{{ item.product.dry_image_ext.dry_object }}" width="50"></td>
        <td>{{ item.dry_order_no }}</td>
        <td>&yen;{{ item.dry_amount }}</td>
        <td>{{ item.dry_add_time }}</td>
        <td>{{ item.status_text }}</td>
        {{# if(item.download != '0'){ }}
        <td><a href="javascript:void(0);" url="/Html/Order/downloadList?id={{ item.id }}" class="download">下载源码</a><br>{{ item.download }}截止</td>
        {{# } else { }}
        <td></td>
        {{# } }}
    </tr>
    {{#  }); }}
    `;
    return code;
}